//
//  UUVerifyCodeVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 12/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUVerifyCodeVC.h"
#import <UUFoundation/DKCountdownManager.h>

@interface UUVerifyCodeVC () <DKCountdownManagerObserver>

@property (nonatomic, strong) UIButton *verifyCodeButton;
@property (nonatomic, weak) DKVerifyCodeView *codeView;

@end

@implementation UUVerifyCodeVC

+ (BOOL)enableTranslucentNavigationBar {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"输入验证码";
    titleLabel.textColor = COMMON_TEXT_DARK_COLOR;
    titleLabel.font = [UIFont systemFontOfSize:21];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom).offset(70);
        make.left.right.mas_equalTo(0);
    }];
    
    self.verifyCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.verifyCodeButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
    [self.verifyCodeButton addTarget:self action:@selector(resendVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.verifyCodeButton];
    [self.verifyCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(titleLabel.mas_bottom);
    }];
    
    [self updateCounting];
    [[DKCountdownManager sharedInstance] addObserver:self];
    
    WEAK_SELF();
    DKVerifyCodeView *codeView = [DKVerifyCodeView new];
    [codeView setCodeDidInputCompleteBlock:^(NSString *code) {
        BLOCK_SAFE_CALLS(weakSelf.codeDidInputCompleteBlock, code);
    }];
    [self.view addSubview:codeView];
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.verifyCodeButton.mas_bottom).offset(50);
    }];
    self.codeView = codeView;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self updateBackBarButtonColor:[UIColor blackColor]];
    [self updateBackBarButtonTitle:nil];
    
    [self.codeView clear];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.codeView becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startCountdown {
    [[DKCountdownManager sharedInstance] startCountdown];
}

- (void)updateCounting {
    if ([[DKCountdownManager sharedInstance] hasCounting]) {
        [self.verifyCodeButton setAttributedTitle:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"已发送至%@，%.0f秒后重发",
                                                                                              self.mobile,
                                                                                              [[DKCountdownManager sharedInstance] currentCounting]]
                                                                                  attributes:@{
                                                                                               NSForegroundColorAttributeName : COMMON_TEXT_LIGHT_COLOR
                                                                                               }]
                                         forState:UIControlStateDisabled];
        self.verifyCodeButton.enabled = NO;
    } else {
        NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已发送至%@，", self.mobile]
                                                                                            attributes:@{
                                                                                                         NSForegroundColorAttributeName : COMMON_TEXT_LIGHT_COLOR
                                                                                                         }];
        [attributedTitle appendAttributedString:[[NSAttributedString alloc] initWithString:@"重新发送"
                                                                                attributes:@{
                                                                                             NSForegroundColorAttributeName : COMMON_TEXT_ORANGE_COLOR
                                                                                             }]];
        [self.verifyCodeButton setAttributedTitle:attributedTitle forState:UIControlStateNormal];
        self.verifyCodeButton.enabled = YES;
    }
}

- (void)resendVerifyCode {
    ShowIndicatorInView(self.view);
    
    WEAK_SELF();
    self.requestVerifyCodeBlock(^(NSError *error) {
        if (error == nil) {
            HideIndicatorInView(weakSelf.view);
            [[DKCountdownManager sharedInstance] startCountdown];
        } else {
            ShowTipsOnView(error.localizedDescription, weakSelf.view);
        }
    });
}

#pragma mark - DKCountdownManagerObserver methods

- (void)countdownManagerDidUpdateCounting {
    [self updateCounting];
}

@end
